/*
 * @lc app=leetcode.cn id=501 lang=javascript
 *
 * [501] 二叉搜索树中的众数
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var findMode = function(root) {
  var map = {}, max = 0, ret = [];

  var dfs = function(root){
    if(!root) return;
    dfs(root.left);

    if(map[root.val]) {
      map[root.val] = map[root.val] + 1;
    } else {
      map[root.val] = 1;
    }

    if(map[root.val] > max) {
      max = map[root.val];
    }
    dfs(root.right);
  }
  
  dfs(root);
  for(var key in map){
    if(map[key] === max){
      ret.push(key)
    }
  }
  return ret;
};
// @lc code=end

